import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

import '../ivy_ezviz_plugin.dart';

enum EZPTZCommand {
  EZPTZCommandUp,
  EZPTZCommandDown,
  EZPTZCommandLeft,
  EZPTZCommandRight,
  EZPTZCommandZoomIn,
  EZPTZCommandZoomOut
}
enum EZPTZAction { EZPTZActionSTART, EZPTZActionSTOP }


///摄像头控制
class EzvizUIKitControl {
  final MethodChannel _channel;
  EventCallback callback;
  EzvizUIKitControl(int id,this.callback):_channel = new MethodChannel("ivy_ezviz_plugin_$id"){
    ///监听
    this.enableWatcherEzviz(id);
    this.listenerEzviz((result) {
      this.callback(result);
      return;
    });
  }
  ///释放资源
  Future<void> dispose() async {
    this.releasePlayer();
    ///销毁监听
    this.disableWatcherEzviz();
  }

  /**
   * 获取当前状态：
   * EZUIPlayer.STATUS_INIT = 0;
   * EZUIPlayer.STATUS_START = 1;
   * EZUIPlayer.STATUS_STOP = 2;
   * EZUIPlayer.STATUS_PLAY = 3;
   * EZUIPlayer.STATUS_PAUSE = 4;
   * @return
   */
  Future<Map> getStatus() async {
    final Map map =
        await _channel.invokeMethod('ezviz.getStatus', null);
    return map;
  }

  ///释放资源
  Future<void> releasePlayer() async {
    final Map map =
        await _channel.invokeMethod('ezviz.releasePlayer', null);
  }

  ///播放
  Future<void> startPlay() async {
    final Map map =
        await _channel.invokeMethod('ezviz.startPlay', null);
  }

  ///停止
  Future<void> stopPlay() async {
    final Map map =
        await _channel.invokeMethod('ezviz.stopPlay', null);
  }

  ///设置播放的url
  Future<void> setUrl(String url) async {
    final Map map = await _channel
        .invokeMethod('ezviz.setUrl', {'url': url.toString()});
  }

  ///暂停暂停播放，回放专用接口
  Future<void> pausePlay() async {
    final Map map =
        await _channel.invokeMethod('ezviz.pausePlay', null);
  }

  ///恢复播放，回放专用接口
  Future<void> resumePlay() async {
    final Map map =
        await _channel.invokeMethod('ezviz.resumePlay', null);
  }

  ///seek播放位置,回放使用
  Future<void> seekPlayback(String date) async {
    final Map map = await _channel
        .invokeMethod('ezviz.seekPlayback', {'date': date});
  }

  /**
   * 动态设置播放区域大小
   * 当width等于0（height等于0）时，播放区域以height（width）为标准，宽高按视频分辨率比例播放
   *
   * @param width  播放区域宽
   * @param height 播放区域高
   */
  Future<void> setSurfaceSize(int weight, int height) async {
    final Map map = await _channel.invokeMethod(
        'ezviz.setSurfaceSize', {'width': weight, 'height': height});
  }

  /**
   * PTZ 控制接口
   * 该接口为耗时操作，必须在线程中调用
      var  deviceSerial   设备序列号
      var  cameraNo        通道号
      var  command ptz控制命令
      var  action 控制启动/停止
      var  speed 速度（0-2）
   * @return 操作成功或者失败(返回失败错误码)
   */
  Future<Map> controlPTZ(String deviceSerial, int cameraNo,
      EZPTZCommand command, EZPTZAction action,
      {int speed = 1}) async {
    final Map map =
        await _channel.invokeMethod('ezviz.controlPTZ', {
      'deviceSerial': deviceSerial,
      'cameraNo': cameraNo,
      'command': command.toString(),
      'action': action.toString(),
      'speed': speed
    });
    print(map);
    return map;
  }

  ///截图
  Future<void> captureCamera(String deviceSerial, int cameraNo) async {
    final Map map =
    await _channel.invokeMethod('ezviz.captureCamera', {'deviceSerial': deviceSerial,
      'cameraNo': cameraNo,});
  }





  ///*******************************************监听******************************************************

  StreamSubscription _eventSubsEzviz;
  EventCallback _voidCallbackEzviz;

  ///标注地点监听
  void enableWatcherEzviz(int id) async {
    if (_eventSubsEzviz == null) {
      _eventSubsEzviz = EventChannel('com.ivy.ezviz.uikit.event_$id')
          .receiveBroadcastStream()
          .listen(_eventListenerEzviz, onError: _errorListener);
    }
  }

  ///标注地点销毁监听
  void disableWatcherEzviz() async {
    _eventSubsEzviz?.cancel();
    _eventSubsEzviz = null;
  }

  ///标注地点监听返回结果
  void _eventListenerEzviz(event) {
    _voidCallbackEzviz(event);
  }

  ///标注地点报错
  void _errorListener(Object obj) {
    print("errorListener: $obj");
  }

  void listenerEzviz(EventCallback callback) {
    _voidCallbackEzviz = callback;
  }
  
}
